package az.mecid.rssreader.server.services;

import az.mecid.rssreader.client.services.SubscribeService;
import az.mecid.rssreader.model.dao.FeedDAO;
import az.mecid.rssreader.model.dao.FeedDAOImpl;
import az.mecid.rssreader.model.dao.SubscribeDAO;
import az.mecid.rssreader.model.dao.SubscribeDAOImpl;
import az.mecid.rssreader.model.entity.FeedEntity;
import az.mecid.rssreader.model.entity.SubscriptionEntity;
import az.mecid.rssreader.model.utils.EMF;
import az.mecid.rssreader.utils.FeedUtils;
import com.google.appengine.api.datastore.Link;
import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: mega
 * Date: 23.02.2010
 * Time: 14:02:27
 */
@SuppressWarnings("unchecked")
public class SubscribeServiceImpl extends RemoteServiceServlet implements SubscribeService {

    private Logger logger = Logger.getLogger(SubscribeServiceImpl.class.getName());

    public boolean subscribeFeed(String url) {
        boolean result = false;

        String patternUrl = FeedUtils.pattern(url);
        String title = FeedUtils.validURL(patternUrl);

        if(title != null){
            FeedDAO feedDAO = new FeedDAOImpl();
            FeedEntity feed = feedDAO.save(url,title);
            SubscribeDAO subscribeDAO = new SubscribeDAOImpl();
            subscribeDAO.save(new SubscriptionEntity(feed.getId(),UserServiceFactory.getUserService().getCurrentUser()));

            result = true;
        }

        return result;
    }
}